import * as YvanUI from "yvan-ui-wotu";
import * as Data from '../data'

const _ = YvanUI.lodash
const React = YvanUI.React

interface Ref {
    sym_symptom: YvanUI.Statistic
    sym_department: YvanUI.Statistic
    sym_disease: YvanUI.Statistic
    sym_medicine: YvanUI.Statistic
    dis_department: YvanUI.Statistic
    dis_medicine: YvanUI.Statistic
}

export default class RecommendSymptom extends YvanUI.Scope<void, Ref> {

    query = this.$$({
        gender: '2',
        symptom: '',
        disease: '',
    })

    main = this.$$({
        symptom: '',
        disease: '',
    })

    result = this.$$({
        sym_symptom: '',
        sym_department: '',
        sym_disease: '',
        sym_medicine: '',
        dis_department: '',
        dis_medicine: ''
    })

    onRender() {
        window['biz'] = this
    }

    diseaseChange() {
        if (!this.query.disease) {
            this.result.dis_department = ''
            this.result.dis_medicine = ''
            return
        }

        // =============== disease_department
        this.refs.dis_department.loading = true
        YvanUI.ajax.func({
            url: '/kg/inference/disease/department',
            method: 'post-json',
            data: {
                disease: this.query.disease,
                gender: parseInt(this.query.gender)
            }

        }).then((res: any) => {
            this.result.dis_department = (!res.result) ? '' : res.result

        }).finally(() => {
            this.refs.dis_department.loading = false
        })

        // =============== disease_medicine
        this.refs.dis_medicine.loading = true
        YvanUI.ajax.func({
            url: '/kg/inference/disease/medicine',
            method: 'post-json',
            data: {
                disease: this.query.disease,
                gender: parseInt(this.query.gender)
            }

        }).then((res: any) => {
            this.result.dis_medicine = (!res.result) ? '' : res.result

        }).finally(() => {
            this.refs.dis_medicine.loading = false
        })
    }

    symptomChange() {
        if (!this.query.symptom) {
            this.result.sym_symptom = ''
            this.result.sym_symptom = ''
            this.result.sym_department = ''
            this.result.sym_disease = ''
            this.result.sym_medicine = ''
            return
        }

        // =============== symptom_symptom
        this.refs.sym_symptom.loading = true
        YvanUI.ajax.func({
            url: '/kg/inference/symptom/symptom',
            method: 'post-json',
            data: {
                symptoms: this.query.symptom.split(','),
                gender: parseInt(this.query.gender)
            }

        }).then((res: any) => {
            this.result.sym_symptom = (!res.result) ? '' : res.result

        }).finally(() => {
            this.refs.sym_symptom.loading = false
        })

        // =============== symptom_department
        this.refs.sym_department.loading = true
        YvanUI.ajax.func({
            url: '/kg/inference/symptom/department',
            method: 'post-json',
            data: {
                symptoms: this.query.symptom.split(','),
                gender: parseInt(this.query.gender)
            }

        }).then((res: any) => {
            this.result.sym_department = (!res.result) ? '' : res.result

        }).finally(() => {
            this.refs.sym_department.loading = false
        })

        // =============== symptom_disease
        this.refs.sym_disease.loading = true
        YvanUI.ajax.func({
            url: '/kg/inference/symptom/disease',
            method: 'post-json',
            data: {
                symptoms: this.query.symptom.split(','),
                gender: parseInt(this.query.gender)
            }

        }).then((res: any) => {
            this.result.sym_disease = (!res.result) ? '' : res.result

        }).finally(() => {
            this.refs.sym_disease.loading = false
        })

        // =============== symptom_medicine
        this.refs.sym_medicine.loading = true
        YvanUI.ajax.func({
            url: '/kg/inference/symptom/medicine',
            method: 'post-json',
            data: {
                symptoms: this.query.symptom.split(','),
                gender: parseInt(this.query.gender)
            }

        }).then((res: any) => {
            this.result.sym_medicine = (!res.result) ? '' : res.result

        }).finally(() => {
            this.refs.sym_medicine.loading = false
        })
    }

    vjson = this.$$({
        view: 'page',
        title: '推理',
        items: [
            {
                view: 'row',
                gutter: 16,
                items: [
                    {
                        view: 'col',
                        span: 24,
                        xl: {span: 12},
                        items: [
                            {
                                view: 'card',
                                title: '症状推理',
                                items: [
                                    {
                                        view: 'form',
                                        items: [
                                            {
                                                view: 'select',
                                                bind: 'query.gender',
                                                label: '性别',
                                                data: [
                                                    {value: '0', label: '男性'},
                                                    {value: '1', label: '女性'},
                                                    {value: '2', label: '不限'}
                                                ]
                                            },
                                            {
                                                view: 'select',
                                                bind: 'query.symptom',
                                                label: '症状',
                                                mode: 'multiple',
                                                dataSource: {
                                                    func: () => {
                                                        return new Promise(resolve => {
                                                            YvanUI.ajax.func({
                                                                url: '/kg/search/data/info',
                                                                method: 'post-json',
                                                                data: {
                                                                    label: 'gender',
                                                                    name: 'symptom',
                                                                    align: false
                                                                }
                                                            }).then((res: any) => {
                                                                console.log(res.result)
                                                                const data = _.map(res.result, (i) => {
                                                                    return {value: i, label: i}
                                                                })
                                                                resolve(data)
                                                            }).finally(() => {

                                                            })
                                                        })
                                                    }
                                                },
                                                onChange: "symptomChange"
                                            }
                                        ]
                                    },
                                    {
                                        view: 'divider'
                                    },
                                    {
                                        view: 'row',
                                        items: [
                                            {
                                                view: 'col',
                                                span: 12,
                                                items: [
                                                    {
                                                        view: 'statistic',
                                                        ref: 'sym_symptom',
                                                        title: '相关症状推荐',
                                                        value: {$watch: () => this.result.sym_symptom},
                                                    },
                                                ]
                                            },
                                            {
                                                view: 'col',
                                                span: 12,
                                                items: [
                                                    {
                                                        view: 'statistic',
                                                        ref: 'sym_department',
                                                        title: '科室推荐',
                                                        value: {$watch: () => this.result.sym_department},
                                                    },
                                                ]
                                            },
                                        ]
                                    },
                                    {
                                        view: 'divider'
                                    },
                                    {
                                        view: 'row',
                                        items: [
                                            {
                                                view: 'statistic',
                                                ref: 'sym_disease',
                                                title: '潜在疾病推荐',
                                                value: {$watch: () => this.result.sym_disease},
                                            },
                                        ]
                                    },
                                    {
                                        view: 'divider'
                                    },
                                    {
                                        view: 'row',
                                        items: [
                                            {
                                                view: 'statistic',
                                                ref: 'sym_medicine',
                                                title: '治疗药品推荐',
                                                value: {$watch: () => this.result.sym_medicine},
                                            },
                                        ]
                                    },
                                ]
                            }
                        ]
                    },
                    {
                        view: 'col',
                        span: 24,
                        xl: {span: 12},
                        items: [
                            {
                                view: 'card',
                                title: '疾病推理',
                                items: [
                                    {
                                        view: 'form',
                                        items: [
                                            {
                                                view: 'select',
                                                bind: 'query.gender',
                                                label: '性别',
                                                data: [
                                                    {value: '0', label: '男性'},
                                                    {value: '1', label: '女性'},
                                                    {value: '2', label: '不限'}
                                                ]
                                            },
                                            {
                                                view: 'select',
                                                bind: 'query.disease',
                                                label: '疾病',
                                                dataSource: {
                                                    func: () => {
                                                        return new Promise(resolve => {
                                                            YvanUI.ajax.func({
                                                                url: '/kg/search/data/info',
                                                                method: 'post-json',
                                                                data: {
                                                                    label: 'gender',
                                                                    name: 'disease',
                                                                    align: false
                                                                }
                                                            }).then((res: any) => {
                                                                const data = _.map(res.result, (i) => {
                                                                    return {value: i, label: i}
                                                                })
                                                                resolve(data)
                                                            }).finally(() => {

                                                            })
                                                        })
                                                    }
                                                },
                                                onChange: this.diseaseChange
                                            }
                                        ]
                                    },
                                    {
                                        view: 'divider'
                                    },
                                    {
                                        view: 'row',
                                        items: [
                                            {
                                                view: 'col',
                                                span: 12,
                                                items: [
                                                    {
                                                        view: 'statistic',
                                                        ref: 'dis_department',
                                                        title: '科室推断',
                                                        value: {$watch: () => this.result.dis_department},
                                                    },
                                                ]
                                            },
                                            {
                                                view: 'col',
                                                span: 12,
                                                items: [
                                                    {
                                                        view: 'statistic',
                                                        ref: 'dis_medicine',
                                                        title: '药品推断',
                                                        value: {$watch: () => this.result.dis_medicine},
                                                    },
                                                ]
                                            }
                                        ]
                                    },

                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    })
}